package com.itboye.sunsun;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.controller.UnLoginState;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsunhome.R;

public class MainHomeActivity extends BaseActivity implements OnClickListener {
	// 三个tab布局
	private RelativeLayout knowLayout, iWantKnowLayout, meLayout;

	// 底部标签切换的Fragment
	private Fragment knowFragment, iWantKnowFragment, meFragment,
			currentFragment;
	// 底部标签图片
	private ImageView knowImg, meImg;
	// 底部标签的文本
	private TextView knowTv, iWantKnowTv, meTv;

	ImageView iWantKnowImg;

	public static DisplayMetrics outMetrics;
	public static int screenWidth;
	// 所需的全部权限
	static final String[] PERMISSIONS = new String[]{
			Manifest.permission.WRITE_SETTINGS,
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStatusBarColor(R.color.home_blue);
		setContentView(R.layout.homeman_activity);
		WindowManager manager = this.getWindowManager();
		outMetrics = new DisplayMetrics();
		manager.getDefaultDisplay().getMetrics(outMetrics);
		isCameraPermission(this,103);
		screenWidth = outMetrics.widthPixels;
		initUI();
		initTab();
	}
	public static boolean isCameraPermission(Activity context, int requestCode) {
		if (Build.VERSION.SDK_INT >= 23) {
			int aPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS);
//			int bPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);
//			int cPermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
//			int dPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
//			int ePermission = ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
			if (aPermission != PackageManager.PERMISSION_GRANTED) {
				ActivityCompat.requestPermissions(context, PERMISSIONS, requestCode);
				return false;
			}
		}
		return true;
	}
	/**
	 * 初始化UI
	 */
	private void initUI() {
		knowLayout = (RelativeLayout) findViewById(R.id.rl_know);
		iWantKnowLayout = (RelativeLayout) findViewById(R.id.rl_want_know);
		meLayout = (RelativeLayout) findViewById(R.id.rl_me);
		knowLayout.setOnClickListener(this);
		iWantKnowLayout.setOnClickListener(this);
		meLayout.setOnClickListener(this);

		knowImg = (ImageView) findViewById(R.id.iv_know);
		iWantKnowImg = (ImageView) findViewById(R.id.iv_i_want_know);
		meImg = (ImageView) findViewById(R.id.iv_me);
		knowTv = (TextView) findViewById(R.id.tv_know);
		iWantKnowTv = (TextView) findViewById(R.id.tv_i_want_know);
		meTv = (TextView) findViewById(R.id.tv_me);
		if ((Boolean) SPUtils.get(App.ctx, null, SPContants.IS_LOGINED, false)) {
			LoginController.setLoginState(new LoginedState());
		} else {
			LoginController.setLoginState(new UnLoginState());
		}
	}

	/**
	 * 初始化底部标签
	 */
	private void initTab() {
		if (knowFragment == null) {
			knowFragment = new HomeFragmentH();
		}

		if (!knowFragment.isAdded()) {
			// 提交事务
			getSupportFragmentManager().beginTransaction()
					.add(R.id.content_layout, knowFragment).commit();

			// 记录当前Fragment
			currentFragment = knowFragment;
			// 设置图片文本的变化
			knowImg.setImageResource(R.drawable.home);
			knowTv.setTextColor(getResources().getColor(R.color.home_blue));
			iWantKnowImg.setImageResource(R.drawable.mendian);
			iWantKnowTv.setTextColor(getResources().getColor(R.color.gray));
			meImg.setImageResource(R.drawable.home_me);
			meTv.setTextColor(getResources().getColor(R.color.gray));

		}

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.rl_know: // 知道
			clickTab1Layout();
			break;
		case R.id.rl_want_know: // 我想知道
			clickTab2Layout();
			break;
		case R.id.rl_me: // 我的
			clickTab3Layout();
			break;
		case R.id.iv_i_want_know:
			clickTab2Layout();
			break;
		default:
			break;
		}
	}

	/**
	 * 点击第一个tab
	 */
	private void clickTab1Layout() {
		if (knowFragment == null) {
			knowFragment = new HomeFragmentH();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				knowFragment);

		// 设置底部tab变化
		knowImg.setImageResource(R.drawable.home);
		knowTv.setTextColor(getResources().getColor(R.color.home_blue));
		iWantKnowImg.setImageResource(R.drawable.mendian);
		iWantKnowTv.setTextColor(getResources().getColor(R.color.gray));
		meImg.setImageResource(R.drawable.home_me);
		meTv.setTextColor(getResources().getColor(R.color.gray));
	}

	/**
	 * 点击第二个tab
	 */
	private void clickTab2Layout() {
		if (iWantKnowFragment == null) {
			iWantKnowFragment = new StoreFragment();
		}
		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				iWantKnowFragment);

		knowImg.setImageResource(R.drawable.home_shouye);
		knowTv.setTextColor(getResources().getColor(R.color.gray));
		iWantKnowImg.setImageResource(R.drawable.mendian);
		iWantKnowTv.setTextColor(getResources().getColor(R.color.gray));
		meImg.setImageResource(R.drawable.home_me);
		meTv.setTextColor(getResources().getColor(R.color.gray));

	}

	/**
	 * 点击第三个tab
	 */
	private void clickTab3Layout() {
		if (meFragment == null) {
			meFragment = new MeFragment();
		}

		addOrShowFragment(getSupportFragmentManager().beginTransaction(),
				meFragment);
		knowImg.setImageResource(R.drawable.home_shouye);
		knowTv.setTextColor(getResources().getColor(R.color.gray));
		iWantKnowImg.setImageResource(R.drawable.mendian);
		iWantKnowTv.setTextColor(getResources().getColor(R.color.gray));
		meImg.setImageResource(R.drawable.home_me_shouye);
		meTv.setTextColor(getResources().getColor(R.color.home_blue));

	}

	/**
	 * 添加或者显示碎片
	 * 
	 * @param transaction
	 * @param fragment
	 */
	private void addOrShowFragment(FragmentTransaction transaction,
			Fragment fragment) {
		if (currentFragment == fragment)
			return;

		if (!fragment.isAdded()) { // 如果当前fragment未被添加，则添加到Fragment管理器中
			transaction.hide(currentFragment)
					.add(R.id.content_layout, fragment).commit();
		} else {
			transaction.hide(currentFragment).show(fragment).commit();
		}

		currentFragment = fragment;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
		}

		return true;
	}
}
